package com.xiaomai.eduservice.controller;


import com.xiaomai.commonutil.R;
import com.xiaomai.eduservice.client.VodClient;
import com.xiaomai.eduservice.entity.EduVideo;
import com.xiaomai.eduservice.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-08-30
 */
@RestController
@RequestMapping("/eduservice/video")

public class EduVideoController {
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private VodClient vodClient;

    //添加小节
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo video){
        eduVideoService.save(video);
        return R.ok();
    }

    //删除小节
    @DeleteMapping("deleteVideo/{videoId}")
    public R removeVideo(@PathVariable String videoId){
        //根据小节id查找视频id并删除视频
        EduVideo eduVideo = eduVideoService.getById(videoId);
        String videoSourceId = eduVideo.getVideoSourceId();
        if(!StringUtils.isEmpty(videoSourceId)){
            vodClient.removeAlyVideo(videoSourceId);
        }
        //删除小节
        eduVideoService.removeById(videoId);
        return R.ok();
    }
    //查找小节
    @GetMapping("selectVideo/{videoId}")
    public R getVideo(@PathVariable String videoId){
        EduVideo video = eduVideoService.getById(videoId);
        return R.ok().data("video",video);
    }

    //修改小节
    @PostMapping("updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.updateById(eduVideo);
         return R.ok();
    }

}

